package com.aaron.gesturehelper.gesturelib;

import java.io.File;
import java.util.ArrayList;

import com.aaron.gesturehelper.R;
import com.aaron.gesturehelper.gesturelib.GestureView.OnGestureListener;
import com.aaron.gesturehelper.gesturelib.GestureView.OnGesturePerformedListener;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class TestGestureActivity extends Activity implements OnGestureListener, OnGesturePerformedListener {
	static class GestureInfo {
		String name;
		Bitmap bitmap;
	}
	
	private MyGestureLibrary mLib;
	private ArrayList<GestureInfo> mAllGestures;
	private GestureView mView;
	private ListView mList;
	
	public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.test_gesture);
		mAllGestures = loadGesture();
		mView = (GestureView) findViewById(R.id.gestureview);
		mList = (ListView) findViewById(R.id.allgesture);
		mList.setAdapter(new MyAdapter(this));
		
		mView.setGestureListener(this);
		mView.setGesturePerformedListener(this);
	}
	
	private ArrayList<GestureInfo> loadGesture() {
		mLib = MyGestureLibrary.fromFile(new File(getFilesDir(),"gestures"));
		
		ArrayList<GestureInfo> list = new ArrayList<GestureInfo>();

		if (mLib.load()) {
            for (String name : mLib.getGestureEntries()) {
                MyGesture gesture = mLib.getGestures(name);
                final Bitmap bitmap = gesture.toBitmap(64, 64, 8, Color.YELLOW);
                GestureInfo info = new GestureInfo();
                info.name = name;
                info.bitmap = bitmap;
                list.add(info);
       
            }
        }
		return list;
	}
	
	private class MyAdapter extends BaseAdapter {

		private LayoutInflater mInflater;
		
		public MyAdapter(Context context) {
			mInflater = LayoutInflater.from(context);
		}
		
		@Override
		public int getCount() {
			return mAllGestures.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView == null) {
				convertView = mInflater.inflate(R.layout.gestureitem, null);
			}
			TextView text = (TextView) convertView.findViewById(R.id.name);
			String name = mAllGestures.get(position).name;

			text.setText(name);
			
			ImageView image = (ImageView) convertView.findViewById(R.id.image);
			
			image.setImageBitmap(mAllGestures.get(position).bitmap);
			
			return convertView;
		}
	}

	@Override
	public void onGesture(GestureView view) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onGestureEnd(GestureView view) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onGestureStart(GestureView view) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onGesturePerformed(MyGesture gesture) {
		ArrayList<MyPrediction> predictions = mLib.recognize(gesture);
		if(!predictions.isEmpty()) {
			MyPrediction p = predictions.get(0);
			Toast.makeText(this, "Name:"+p.name+"   score:"+p.score, Toast.LENGTH_SHORT).show();
		}
		
	}

	public void onDestroy() {
		super.onDestroy();
		mLib = null;
	}
}
